package com.kkb.servlet;

import com.alibaba.fastjson.JSONObject;
import com.kkb.bean.Student;
import com.kkb.enumeration.Sex;
import com.kkb.enumeration.StudentState;
import com.kkb.pojo.Page;
import com.kkb.pojo.ResponseData;
import com.kkb.service.StudentService;
import com.kkb.service.impl.StudentServiceImpl;
import com.kkb.util.EnumUtils;
import com.kkb.util.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * 学生servlet
 */
@WebServlet("/student")
public class StudentServlet extends HttpServlet {
    private StudentService service = new StudentServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        switch (method) {
            case "query":
                query(req, resp);
                break;
            case "getById":
                getById(req, resp);
                break;
            case "insert":
                insert(req, resp);
                break;
            case "update":
                update(req, resp);
                break;
            case "delete":
                delete(req, resp);
                break;
            case "checkNumber":
                checkNumber(req, resp);
                break;
            default:
                break;
        }
    }

    protected void query(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 处理参数
        String pageIndex = req.getParameter("pageIndex");
        String pageSize = req.getParameter("pageSize");
        String name = req.getParameter("name");
        String number = req.getParameter("number");
        String sex = req.getParameter("sex");
        Page page = new Page(Integer.parseInt(pageIndex), Integer.parseInt(pageSize));
        Student stu = new Student();
        stu.setName(name);
        stu.setNumber(number);
        if (StringUtils.nonBlank(sex)) {
            stu.setSex(EnumUtils.getEnumByValue(Sex.class, Integer.parseInt(sex)));
        }
        // 查询列表
        List<Student> stuList = service.getAll(stu, page);
        // 查询总数
        page.setTotal(service.count(stu));

        // 返回数据
        ResponseData respData = new ResponseData();
        respData.getData().put("stuList", stuList);
        respData.getData().put("page", page);
        resp.setContentType("text/html;charset=utf-8");
        resp.getWriter().write(JSONObject.toJSONString(respData));
    }

    protected void getById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        Student stu = service.getById(Integer.parseInt(id));
        ResponseData respData = new ResponseData("student", stu);
        resp.setContentType("text/html;charset=utf-8");
        resp.getWriter().write(JSONObject.toJSONString(respData));
    }

    protected void insert(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Student stu = new Student();
        stu.setName(req.getParameter("name"));
        stu.setNumber(req.getParameter("number"));
        stu.setGradeId(Integer.parseInt(req.getParameter("grade")));
        stu.setSex(EnumUtils.getEnumByValue(Sex.class, Integer.parseInt(req.getParameter("sex"))));
        stu.setPhone(req.getParameter("phone"));
        stu.setEmail(req.getParameter("email"));
        stu.setRegistered(req.getParameter("registered"));
        stu.setAddress(req.getParameter("address"));
        stu.setProfession(req.getParameter("profession"));
        stu.setPolitics(req.getParameter("politics"));
        stu.setIdNo(req.getParameter("idNo"));
        stu.setIntroduction(req.getParameter("introduction"));

        stu.setRegDate(new Date());
        stu.setState(StudentState.READING);
        // 插入
        int rows = service.insert(stu);
        // 返回信息
        ResponseData respData;
        if (rows > 0) {
            respData = new ResponseData(true, "新增成功");
        } else {
            respData = new ResponseData(false, "服务器忙，新增失败");
        }
        resp.setContentType("text/html;charset=utf-8");
        resp.getWriter().write(JSONObject.toJSONString(respData));
    }

    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Student stu = new Student();
        stu.setName(req.getParameter("name"));
        stu.setNumber(req.getParameter("number"));
        stu.setGradeId(Integer.parseInt(req.getParameter("grade")));
        stu.setSex(EnumUtils.getEnumByValue(Sex.class, Integer.parseInt(req.getParameter("sex"))));
        stu.setPhone(req.getParameter("phone"));
        stu.setEmail(req.getParameter("email"));
        stu.setRegistered(req.getParameter("registered"));
        stu.setAddress(req.getParameter("address"));
        stu.setProfession(req.getParameter("profession"));
        stu.setPolitics(req.getParameter("politics"));
        stu.setIdNo(req.getParameter("idNo"));
        stu.setIntroduction(req.getParameter("introduction"));

        stu.setId(Integer.parseInt(req.getParameter("id")));
        // 修改
        int rows = service.update(stu);
        // 返回信息
        ResponseData respData;
        if (rows > 0) {
            respData = new ResponseData(true, "修改成功");
        } else {
            respData = new ResponseData(false, "服务器忙，修改失败");
        }
        resp.setContentType("text/html;charset=utf-8");
        resp.getWriter().write(JSONObject.toJSONString(respData));
    }

    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        // 删除
        int rows = service.delete(Integer.parseInt(id));
        // 返回信息
        ResponseData respData;
        if (rows > 0) {
            respData = new ResponseData(true, "删除成功");
        } else {
            respData = new ResponseData(false, "服务器忙，删除失败");
        }
        resp.setContentType("text/html;charset=utf-8");
        resp.getWriter().write(JSONObject.toJSONString(respData));
    }

    protected void checkNumber(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String idStr = req.getParameter("id");
        String number = req.getParameter("number");
        Integer id = null;
        if (StringUtils.nonBlank(idStr)){
            id = Integer.parseInt(idStr);
        }
        boolean exist = service.checkNumber(number, id);
        ResponseData respData = new ResponseData("exist", exist);
        resp.setContentType("text/html;charset=utf-8");
        resp.getWriter().write(JSONObject.toJSONString(respData));
    }
}
